﻿using System.Data;
using System;
using System.Collections.Generic;

namespace Home.Haushalt.Lib.Element
{
    /// <summary>
    /// 
    /// </summary>
    [ElementExtension("Element", 1)]
    public class Opponent : ElementBase
    {
        #region CONSTANTS
        public new static readonly Type ElementType = typeof(Opponent);

        /// <summary></summary>
        public class Fields
        {
            public const string Id = ELEMENTPROPERTY_ID;
            public const string Label = ELEMENTPROPERTY_LABEL;
            public const string Description = ELEMENTPROPERTY_NOTE;
            public const string Name = "n";
            public const string Zusatzname = "v";
            public const string Ident = "d";
            public const string OpponentTypeId = "o";

            public const string UnboundFullName = "FullName";
        }
        #endregion CONSTANTS

        /// <summary></summary>
        public override string PropertyFormCaption { get { return (Row != null) ? Row[Fields.Name].ToString() : ""; } }

        /// <summary>
        /// 
        /// </summary>
        public Opponent() : base() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentDataRow"></param>
        public Opponent(DataRow parentDataRow) : base(parentDataRow) { }

        #region OVERRIDES

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int Delete()
        {
            //Unterbojekte löschen
            //Posten
            foreach (DataRow row in DAO.GetSubRows(Posten.ElementType, Posten.Fields.OpponentId, ID))
            {
                new Posten(row).Delete();
            }

            //Member
            foreach (DataRow row in DAO.GetSubRows(Member.ElementType, Member.Fields.OpponentId, ID))
            {
                new Member(row).Delete();
            }

            //Documente
            foreach (DataRow row in DAO.GetSubRows(Document.ElementType, Document.Fields.OpponentId, ID))
            {
                new Document(row).Delete();
            }

            //Date
            foreach (DataRow row in DAO.GetSubRows(Date.ElementType, Date.Fields.OpponentId, ID))
            {
                new Date(row).Delete();
            }

            return base.Delete();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override List<string> InputValidate()
        {
            List<string> result = new List<string>();

            if (BindingRow[Fields.Name].ToString().Trim().Length == 0) result.Add(GetInputValidateMessage(this.GetType(), Fields.Name, "missing"));
            if (BindingRow[Fields.OpponentTypeId].ToInt() == -1) result.Add(GetInputValidateMessage(this.GetType(), Fields.OpponentTypeId, "missing"));
            if (BindingRow[Fields.Ident].ToInt() == -1) result.Add(GetInputValidateMessage(this.GetType(), Fields.Ident, "missing"));

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Dictionary<string, object> CalculateUnboundFields()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            if (CheckColumnsInRow(Fields.Id))
            {
                //Summe
                string fullname = "";
                if (Row[Fields.OpponentTypeId].ToInt() == 1)
                {
                    fullname = Row[Fields.Name].ToString() + ((Row[Fields.Zusatzname].ToString().Length>0)?(", " + Row[Fields.Zusatzname].ToString()):"");
                }
                else
                {
                    fullname = Row[Fields.Name].ToString();
                }

                result.Add(Fields.UnboundFullName, fullname);
            }

            return result;
        }

        #endregion OVERRIDES
    }
}
